package com.jarvis.pzz.modules.setup;

import android.Manifest;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jarvis.pzz.MainActivity;
import com.jarvis.pzz.R;
import com.jarvis.pzz.WebViewActivity;
import com.jarvis.pzz.common.AppManager;
import com.jarvis.pzz.common.BaseActivity;
import com.jarvis.pzz.common.ResponseData;
import com.jarvis.pzz.modules.login.LoginActivity;
import com.jarvis.pzz.modules.sourceshop.SourceShopActivity;
import com.jarvis.pzz.seervers.RequestService;
import com.jarvis.pzz.util.CleanMessageUtil;
import com.jarvis.pzz.util.IntentUtil;
import com.jarvis.pzz.util.SharePreferenceKey;
import com.jarvis.pzz.util.SharePreferenceUtils;
import com.jarvis.pzz.util.VersionManager;
import com.jarvis.pzz.util.net.RequestApi;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.functions.Action1;

public class SetupActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.lin_title)
    RelativeLayout lin_title;
    @BindView(R.id.rel_left)
    RelativeLayout rel_left;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.ll_cache)
    LinearLayout ll_cache;
    @BindView(R.id.ll_about)
    LinearLayout ll_about;
    @BindView(R.id.ll_version)
    LinearLayout ll_version;
    @BindView(R.id.tv_version)
    TextView tv_version;
    @BindView(R.id.tv_cache)
    TextView tv_cache;
    @BindView(R.id.tv_determine)
    TextView tv_determine;

    private RequestService service;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setup;
    }

    @Override
    public void initData() {

        listener();
    }


    @Override
    public void initView() {
        service = RequestApi.create(RequestService.class);
        ButterKnife.bind(this);
        tv_title.setText("设置");
        try {
            tv_cache.setText(CleanMessageUtil.getTotalCacheSize(mActivity));
        } catch (Exception e) {
            e.printStackTrace();
        }
        tv_version.setText(VersionManager.getVersionName(mActivity));
        if(isLogin()){
            tv_determine.setVisibility(View.VISIBLE);
        }else {
            tv_determine.setVisibility(View.GONE);
        }
        setBar();

    }

    private void listener() {
        rel_left.setOnClickListener(this);
        ll_cache.setOnClickListener(this);
        ll_about.setOnClickListener(this);
        ll_version.setOnClickListener(this);
        tv_determine.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_determine:
                showDialog("loading");
                Call<ResponseData> call = service.loginOut();
                call.enqueue(new Callback<ResponseData>() {
                    @Override
                    public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                        disMiss();
                        if (response.body() != null) {
                            int resultCode = response.body().getResultCode();
                            if (resultCode == 200) {
                                MobclickAgent.onProfileSignOff();
                                AppManager.getAppManager().finishAllActivity();
                                SharePreferenceUtils.remove(SharePreferenceKey.LOGIN_MODEL);
                                SharePreferenceUtils.remove(SharePreferenceKey.USER_ID);
                                startActivity(MainActivity.class);
                            } else {
                                showTextToast(response.body().getMessage());
                            }
                        } else {
                            showTextToast("请求失败");
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseData> call, Throwable t) {
                        disMiss();
                        showTextToast(RequestApi.detailError(t));
                    }
                });
                break;
            case R.id.rel_left:
                finish();
                break;
            case R.id.ll_cache:
              //清除缓存
                CleanMessageUtil.clearAllCache(mActivity);
                try {
                    tv_cache.setText(CleanMessageUtil.getTotalCacheSize(mActivity));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.ll_about:
               //关于我们
                Bundle bundle = new Bundle();
                bundle.putString("url", "https://app.pzhaozhao.com:9100/upload/linkHtml/aboutUs.html");
                bundle.putString("title","关于我们");
                bundle.putString("type","1");
                startActivity(WebViewActivity.class, bundle);
                break;
            case R.id.ll_version:
               //更新

                break;
        }
    }


    private void setBar() {
        lin_title.setVisibility(View.VISIBLE);
        lin_title.getBackground().setAlpha(255);
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(lin_title.getLayoutParams());
        lp.setMargins(0, statusBarHeight1, 0, 0);
        lin_title.setLayoutParams(lp);
    }


}
